package com.queclink.ycm.controller;

import com.alibaba.fastjson.JSONObject;
import com.queclink.middleware.Parser;
import com.queclink.ycm.common.utils.HexUtils;
import com.queclink.ycm.entity.Report;
import org.springframework.web.bind.annotation.*;


/**
 * @author jim.li
 * @email jim.li@queclink.com
 * @date 2021-12-24 15:51:00
 */
@RestController
@RequestMapping()
public class CombineController {

    // 用于测试
    @PostMapping("/combine")
    public String combine(@RequestBody JSONObject dataIDJsonObject) {
        try {
            String combine = "";

//            JSONObject dataIDJsonObject = JSONObject.parseObject(msg);
            Report report = new Report();
            combine = report.combine(dataIDJsonObject);

//            JSONArray jsonArray = JSONArray.parseArray(msg);
//            for (Object o : jsonArray) {
//                JSONObject dataIDJsonObject = JSONObject.parseObject(o.toString());
//                Report report = new Report();
//                combine = report.combine(dataIDJsonObject);
//            }
            return combine;
        } catch (Exception e) {
            e.printStackTrace();
            return "failed";
        }
    }

    @PostMapping("/parser")
    public String combine(@RequestParam("msg") String msg) {
        try {
            byte[] buf = HexUtils.hexStr2bytes(msg);
            String jsonString = Parser.toJSONString(buf, buf.length);
            return jsonString;
        } catch (Exception e) {
            e.printStackTrace();
            return "failed";
        }
    }


}
